#!/usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fileencoding=utf-8
# vim:tabstop=4
#
# Copyright (c) 2010 by xkhhdx
#
# Licensed under the Apache License, Version 2.0 (the "License")
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

import urllib, urllib2

jsonstr = ""
__api__ = ""

sourcespname = { "sina" : "新浪",
    "twitter" : "Twitter", }

import logging

def fetch(username, since_id=None, source = "sina"):
    u""" 同步 """
    if source == "sina":
        if since_id:
            timeline_uri="http://api.t.sina.com.cn/statuses/user_timeline/%s.json?source=%s&count=5&since_id=%s" % ( username, __api__, since_id )
        else:
            timeline_uri="http://api.t.sina.com.cn/statuses/user_timeline/%s.json?source=%s&count=5" % ( username, __api__ )

    elif source == "twitter":
    	
        if since_id:
            timeline_uri="http://api.t.sina.com.cn/statuses/user_timeline/xkhhdx.json?source=%s&count=5&since_id=%s" % ( __api__, since_id )
        else:
            timeline_uri="http://api.t.sina.com.cn/statuses/user_timeline/xkhhdx.json?source=%s&count=5" % ( __api__ )

        if since_id:
            timeline_uri="http://twitter.com/statuses/user_timeline/%s.xml?since_id=%s" % ( username, since_id )
        else:
            timeline_uri="http://twitter.com/statuses/user_timeline/%s.xml" % ( username )
            
    logging.info("urtimeline_uri = "+ timeline_uri + "; source= " + source)
    
    values ={}
    data = urllib.urlencode(values)
    req = urllib2.Request(timeline_uri, data)
    response = urllib2.urlopen(req)
    jsonstr = response.read()

    logging.info("jsonstr = "+ jsonstr + "; source= " + source)
    
    return jsonstr, source

if __name__ == "__main__":
    print fetch()

